/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package br.com.jr2tecnologia.model.persistence;

import br.com.jr2tecnologia.LoggerFactory;
import br.com.jr2tecnologia.model.entity.ClientPhone;
import br.com.jr2tecnologia.model.entity.Company;
import br.com.jr2tecnologia.model.entity.Login;
import br.com.jr2tecnologia.model.repository.ClientPhoneRepository;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.persistence.NoResultException;
import javax.persistence.NonUniqueResultException;
import javax.persistence.Query;

/**
 *
 * @author ronildo
 */
public class ClientPhoneDAO  extends GenericDAO<ClientPhone, ClientPhoneRepository> implements ClientPhoneRepository {

    private static final Logger logger = LoggerFactory.getLogger();

    public ClientPhone findByPhone(String phone){
        try{
            return (ClientPhone)em.createNamedQuery("ClientPhone.findByPhone")
                    .setParameter("phone", phone)
                    .getSingleResult();
        }catch(NoResultException e){
            logger.info("Cannot find data ClientPhone by phone: " + phone);
            return null;
        }catch(NonUniqueResultException e){
            logger.severe("Have more than one result");
            throw new DAOException(e);
        }catch(IllegalStateException e){
            logger.severe(msg);
            throw new DAOException(e);
        }
    }

    public List<ClientPhone> list(ClientPhone clientPhone){
        try{
            Company company = clientPhone.getCompany();
            Login login = clientPhone.getLogin();
            String hql = "select clientPhone from ClientPhone as clientPhone ";
            hql+=" left outer join clientPhone.company as company";
            hql+=" left outer join clientPhone.login as login";
            hql+=" where clientPhone.pkClientPhone = clientPhone.pkClientPhone";

            if(clientPhone.getName() != null && !clientPhone.getName().equals(""))
                hql+=" and clientPhone.name like :clientPhone_name";
            if(clientPhone.getPhone() != null && !clientPhone.getPhone().equals(""))
                hql+=" and clientPhone.phone like :clientPhone_phone";
            if(company != null && company.getPkCompany() != null)
                hql+=" and company.pkCompany = :pkCompany";
            if(login != null && login.getPkLogin() != null)
                hql+=" and login.pkLogin = :pkLogin";
            hql+=" order by clientPhone.name, clientPhone.phone";

            Query query = em.createQuery(hql);
            if(clientPhone.getName() != null && !clientPhone.getName().equals(""))
                query.setParameter("clientPhone_name", clientPhone.getName()+"%");
            if(clientPhone.getPhone() != null && !clientPhone.getPhone().equals(""))
                query.setParameter("clientPhone_phone", clientPhone.getPhone()+"%");
            if(company != null && company.getPkCompany() != null)
                query.setParameter("pkCompany", company.getPkCompany());
            if(login != null && login.getPkLogin() != null)
                query.setParameter("pkLogin", login.getPkLogin());

            return query.getResultList();
        }catch(RuntimeException e){
            logger.log(Level.SEVERE, "Error to list client phone",e);
            throw new DAOException(e);
        }
    }

    public List<ClientPhone> listAll(){
        try{
            return em.createNamedQuery("ClientPhone.findAll").getResultList();
        }catch(IllegalStateException e){
            logger.log(Level.SEVERE, msg, e);
            throw new DAOException(e);
        }
    }
}
